/**
 * Created by chengcong on 2017/11/20.
 */
$('.form_datetime').datetimepicker({
    //language:  'fr',
    weekStart: 1,
    todayBtn:  1,
    autoclose: 1,
    todayHighlight: 1,
    startView: 2,
    forceParse: 0,
    showMeridian: 1
});
$('.detailbtn').on('click',function (e) {
    console.log(1111);
    self.location='../../postpaper/postpaper.html?detailId=123456'
});
// const sokceturl = "http://47.90.123.205:8000";
$(function(){
    var msg=window.sessionStorage['user_msg'];
    msg=JSON.parse(msg);
    socket.emit('login',{
        username :msg.username,
        openid  :null,
        sex : 0,
        marking :1,//1pc  2wx  3app  Number
        userrole :msg.userrole,//登陆角色 (医生(1)，用户(2)，助理(3)，药师(4),匿名用户(5))
        token:msg.token
    });
    var msg=window.sessionStorage['user_msg'];
    msg=JSON.parse(msg);
    console.log(msg);
    socket.on('private_msg', function(data) {
        console.log(data);
        window.top.document.getElementsByClassName('unfinish2')[0].innerHTML =
            parseFloat(window.top.document.getElementsByClassName('unfinish2')[0].innerHTML) + 1;
        window.top.document.getElementsByClassName('unfinish2')[0].style.display = 'inline-block';
    })
    $.ajax({
        url:url+'/server/pharmacistFinish',
        type:'post',
        data:{
            id: msg.userid,
            limit:10,
            page:1
        },
         headers: {
          'Authorization': 'Bearer ' + msg.token
          },
        success:function (data) {
            console.log(data);
            if(data.code==200){
                $('table tbody').html('');
                if(data.data.length>0){
                    var finishlist=[];
                    for(var k=0;k<data.data.length;k++){
                        finishlist.unshift(data.data[k]);
                    }
                    var n=Math.ceil(data.count/10);
                    if(n>5){
                        var html='<li><a href="#" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>';
                            html+='<li class="active"><a href="#">1</a></li>' +
                            '<li><a href="#">2</a></li><li><a href="#">3</a></li>' +
                                '<li><a href="#">4</a></li><li><a href="#">5</a></li>';
                            html+='<li><a href="#" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>';
                            $('.pagination').html(html);
                    }else{
                        var html='';
                        for(var a=0;a<n;a++){
                            if(a==0){
                                html+='<li class="active"><a href="#">'+(a+1)+'</a></li>'
                            }else{
                                html+='<li><a href="#">'+(a+1)+'</a></li>'
                            }

                        }
                        html+='';
                        $('.pagination').html(html);
                    }
                    for(var i=0;i<finishlist.length;i++){
                        $('table tbody').append('<tr><td>'+10086+'</td><td>'+finishlist[i].doctorName+'</td>' +
                            '<td>处方已审核</td>' +
                            '<td><button class="btn btn-danger detailbtn" listid="'+finishlist[i]._id+'" status="3">查看详情</button></td></tr>');
                    }
                    $('.pagination a').on('click',function (e) {
                        console.log($(e.target).text());
                        if($(e.target).text()=='下一页'){
                            if($('li.active a').text()==n ){
                                alert('已经到头了');
                            }else if(parseInt($('.pagination li:nth-child(6) a').text())==n){
                                $('li.active').next().addClass('active');
                                $($('li.active')[1]).prev().removeClass('active');
                            }else{
                                $('.pagination li:nth-child(6) a').text(parseInt($('.pagination li:nth-child(6) a').text())+1);
                                $('.pagination li:nth-child(5) a').text(parseInt($('.pagination li:nth-child(5) a').text())+1);
                                $('.pagination li:nth-child(4) a').text(parseInt($('.pagination li:nth-child(4) a').text())+1);
                                $('.pagination li:nth-child(3) a').text(parseInt($('.pagination li:nth-child(3) a').text())+1);
                                $('.pagination li:nth-child(2) a').text(parseInt($('.pagination li:nth-child(2) a').text())+1);
                            }
                            $.ajax({
                                url: url + '/server/pharmacistFinish',
                                type: 'post',
                                data: {
                                    id: msg.userid,
                                    limit: 10,
                                    page: $('li.active a').text()
                                },
                                headers: {
                                    'Authorization': 'Bearer ' + msg.token
                                },
                                success: function (data) {
                                    console.log(data);
                                    if (data.code == 200) {
                                        $('table tbody').html('');
                                        if (data.data.length > 0) {
                                            var finishlist = [];
                                            for (var k = 0; k < data.data.length; k++) {
                                                finishlist.unshift(data.data[k]);
                                            }
                                            for (var i = 0; i < finishlist.length; i++) {
                                                $('table tbody').append('<tr><td>' + 10086 + '</td><td>' + finishlist[i].doctorName + '</td>' +
                                                    '<td>处方已审核</td>' +
                                                    '<td><button class="btn btn-danger detailbtn" listid="' + finishlist[i]._id + '" status="3">查看详情</button></td></tr>');
                                            }
                                            $('.detailbtn').on('click',function (e) {
                                                console.log(1111);
                                                self.location='../../postpaper/postpaper.html?detailId='+$(e.target).attr('listid')+'&&type='+$(e.target).attr('status');
                                            });
                                        }
                                    }
                                }
                            });
                        }else if($(e.target).text()=='上一页'){
                            if($('li.active a').text()==1 ){
                                alert('已经到头了');
                            }else if(parseInt($('.pagination li:nth-child(2) a').text())==1){
                                $('li.active').prev().addClass('active');
                                $($('li.active')[0]).next().removeClass('active');
                            }else{
                                $('.pagination li:nth-child(6) a').text(parseInt($('.pagination li:nth-child(6) a').text())-1);
                                $('.pagination li:nth-child(5) a').text(parseInt($('.pagination li:nth-child(5) a').text())-1);
                                $('.pagination li:nth-child(4) a').text(parseInt($('.pagination li:nth-child(4) a').text())-1);
                                $('.pagination li:nth-child(3) a').text(parseInt($('.pagination li:nth-child(3) a').text())-1);
                                $('.pagination li:nth-child(2) a').text(parseInt($('.pagination li:nth-child(2) a').text())-1);
                            }
                            $.ajax({
                                url: url + '/server/pharmacistFinish',
                                type: 'post',
                                data: {
                                    id: msg.userid,
                                    limit: 10,
                                    page: $('li.active a').text()
                                },
                                headers: {
                                    'Authorization': 'Bearer ' + msg.token
                                },
                                success: function (data) {
                                    console.log(data);
                                    if (data.code == 200) {
                                        $('table tbody').html('');
                                        if (data.data.length > 0) {
                                            var finishlist = [];
                                            for (var k = 0; k < data.data.length; k++) {
                                                finishlist.unshift(data.data[k]);
                                            }
                                            for (var i = 0; i < finishlist.length; i++) {
                                                $('table tbody').append('<tr><td>' + 10086 + '</td><td>' + finishlist[i].doctorName + '</td>' +
                                                    '<td>处方已审核</td>' +
                                                    '<td><button class="btn btn-danger detailbtn" listid="' + finishlist[i]._id + '" status="3">查看详情</button></td></tr>');
                                            }
                                            $('.detailbtn').on('click',function (e) {
                                                console.log(1111);
                                                self.location='../../postpaper/postpaper.html?detailId='+$(e.target).attr('listid')+'&&type='+$(e.target).attr('status');
                                            });
                                        }
                                    }
                                }
                            });
                        }else{
                            $(e.target).parent().addClass('active');
                            $(e.target).parent().siblings().removeClass('active');
                            $.ajax({
                                url: url + '/server/pharmacistFinish',
                                type: 'post',
                                data: {
                                    id: msg.userid,
                                    limit: 10,
                                    page: $(e.target).text()
                                },
                                headers: {
                                    'Authorization': 'Bearer ' + msg.token
                                },
                                success: function (data) {
                                    console.log(data);
                                    if (data.code == 200) {
                                        $('table tbody').html('');
                                        if (data.data.length > 0) {
                                            var finishlist = [];
                                            for (var k = 0; k < data.data.length; k++) {
                                                finishlist.unshift(data.data[k]);
                                            }
                                            for (var i = 0; i < finishlist.length; i++) {
                                                $('table tbody').append('<tr><td>' + 10086 + '</td><td>' + finishlist[i].doctorName + '</td>' +
                                                    '<td>处方已审核</td>' +
                                                    '<td><button class="btn btn-danger detailbtn" listid="' + finishlist[i]._id + '" status="3">查看详情</button></td></tr>');
                                            }
                                            $('.detailbtn').on('click',function (e) {
                                                console.log(1111);
                                                self.location='../../postpaper/postpaper.html?detailId='+$(e.target).attr('listid')+'&&type='+$(e.target).attr('status');
                                            });
                                        }
                                    }
                                }
                            });
                        }                    })

                    $('.detailbtn').on('click',function (e) {
                        console.log(1111);
                        self.location='../../postpaper/postpaper.html?detailId='+$(e.target).attr('listid')+'&&type='+$(e.target).attr('status');
                    });

                }else{
                    $('table tbody').append('<tr><td colspan="4"><img src="../../img/empty-b.png" alt="" style="margin-top: 200px"></td></tr>');
                }
            }else{
                alert(data.mes)
            }
        }
    });
})